package pro1.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pro1.dao.DepartmentMapper;
import pro1.model.Department;
import pro1.model.ResponseBody;
import pro1.service.DepartmentService;

@Service
public class DepartmentServiceImpl implements DepartmentService {

	@Autowired
	DepartmentMapper departmentMapper;

	@Override
	public ResponseBody allDep() {

		return new ResponseBody(1, "成功", departmentMapper.selectAll());
	}

	@Override
	public ResponseBody addOrUpdateDep(Integer depId, String name, String info) {
		ResponseBody response = new ResponseBody(1, "成功");
		if (depId == null) {
			Department dep = departmentMapper.selectByName(name);
			if (dep == null) {
				dep = new Department();
				dep.setName(name);
				dep.setInfo(info);
				departmentMapper.insertSelective(dep);
			} else {
				response = new ResponseBody(0, "该部门已存在");
			}
		} else {
			Department dep = departmentMapper.selectByPrimaryKey(depId);
			if (dep == null) {
				response = new ResponseBody(0, "该部门不存在");
			} else {
				dep.setName(name);
				dep.setInfo(info);
				departmentMapper.updateByPrimaryKeySelective(dep);
			}
		}
		return response;
	}

	@Override
	public ResponseBody deleteDep(Integer depId) {
		ResponseBody response = new ResponseBody(1, "成功");
		Department dep = departmentMapper.selectByPrimaryKey(depId);
		if (dep == null) {
			response = new ResponseBody(0, "该部门不存在");
		} else {
			departmentMapper.deleteByPrimaryKey(depId);
		}
		return response;
	}

}
